package edu.umd.ion.wavemeter.server.jep;

import java.util.Stack;

import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

public class Triplestep extends PostfixMathCommand {
	public Triplestep() {
		numberOfParameters = 4;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void run(Stack inStack) throws ParseException {
		   // check the stack
		   checkStack(inStack);

		   // get the parameter from the stack
		   Double val3 = (Double) inStack.pop();
		   Double val2 = (Double) inStack.pop();
		   Double val1 = (Double) inStack.pop();
		   Double time = (Double) inStack.pop();

		   // calculate the result
		   time -= Math.floor(time);
		   if (time>0.666) {
			   inStack.push(new Double(val3)); 			   	
		   } else if (time>0.333) {
			   inStack.push(new Double(val2)); 			   				  
		   } else {
			   inStack.push(new Double(val1)); 			   				   
		   }		   
		   // push the result on the inStack
	}

}
